﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;

namespace ViewMaker.Core.Controls
{
    /// <summary>
    /// テキストボックスViewコントロール
    /// </summary>
    public class TextBoxViewControl : HeaderViewControl
    {
        /// <summary>
        /// プロパティ定数
        /// </summary>
        public new class Properties : HeaderViewControl.Properties
        {
            public const string StringFormat = "StringFormat";
            public const string IsReadOnly = "IsReadOnly";
            public const string IsMultiline = "IsMultiline";
            public const string IsEmptyAsNull = "IsEmptyAsNull";
            public const string TextAlignment = "TextAlignment";
            public const string LostFocusCommand = "LostFocusCommand";
            public const string EnterKeyCommand = "EnterKeyCommand";
        }

        /// <summary>
        /// 出力フォーマット
        /// </summary>
        public string StringFormat { get; set; }

        /// <summary>
        /// 読み取り専用
        /// </summary>
        [DefaultValue(false)]
        public bool IsReadOnly { get; set; }

        /// <summary>
        /// 複数行
        /// </summary>
        [DefaultValue(false)]
        public bool IsMultiline { get; set; }

        /// <summary>
        /// 空文字をnull扱いするか
        /// </summary>
        [DefaultValue(false)]
        public bool IsEmptyAsNull { get; set; }

        /// <summary>
        /// テキスト配置位置
        /// </summary>
        [Display(Name = "Text Align", Description = "Text Alignment", GroupName = LayoutGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "HorizontalAlignmentList")]
        public LayoutHorizontalAlignment? TextAlignment { get; set; }

        /// <summary>
        /// ロストフォーカス時に実行するコマンド
        /// </summary>
        [Display(GroupName = CommandGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Item.CommandList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string LostFocusCommand { get; set; }

        /// <summary>
        /// Enterキーを押下した時に実行するコマンド
        /// </summary>
        [Display(GroupName = CommandGroup)]
        [View(ViewControlType.ComboBox)]
        [ViewProperty(ComboBoxViewControl.Properties.ItemsSource, "Item.CommandList")]
        [ViewProperty(ComboBoxViewControl.Properties.IsEditable, true)]
        public string EnterKeyCommand { get; set; }

        /// <summary>
        /// コンストラクタ
        /// </summary>
        public TextBoxViewControl()
        {
            ControlType = ViewControlType.TextBox;
        }

        /// <summary>
        /// Xamlを生成する
        /// </summary>
        /// <param name="parent"></param>
        /// <returns></returns>
        public override List<XamlNode> CreateXaml(XamlNode parent)
        {
            var list = base.CreateXaml(parent);
            var node = parent.CreateElement("TextBox");
            if (IsReadOnly) node.SetAttribute("IsReadOnly", IsReadOnly.ToString());
            if (this.IsMultiline)
            {
                node.SetAttribute("TextWrapping", "Wrap");
                node.SetAttribute("AcceptsReturn", "True");
                node.SetAttribute("VerticalScrollBarVisibility", "Auto");
            }
            if (!string.IsNullOrEmpty(Item.BindPath))
            {
                var bindData = Item.BindPath;
                if (!string.IsNullOrEmpty(StringFormat)) bindData += ", StringFormat=" + GetStringFormat(StringFormat);
                if (IsEmptyAsNull) bindData += ", TargetNullValue=''";
                SetBindingAttribute(node, "Text", bindData, Item.IsOneWay);
            }
            if (TextAlignment.HasValue) node.SetAttribute("TextAlignment", TextAlignment.ToString());
            if (!string.IsNullOrEmpty(LostFocusCommand)) AddBehavior(node, "LostFocusBehavior", LostFocusCommand);
            if (!string.IsNullOrEmpty(EnterKeyCommand)) AddBehavior(node, "EnterKeyBehavior", EnterKeyCommand);
            ApplyBaseProperty(node);
            list.Add(node);
            return list;

        }
    }

}
